﻿Imports System.Xml

Public Class frmOrganization
    Public _dir As String
    Public statusnew As Boolean   ' false: Open; True: New
    Public path As String  ' path file xml
    Dim sdataset As DataSet
    Dim dTable As DataTable

    Private Sub dgvResult_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Title.Text = dgvResult(dgvResult.Columns("Title").Index, dgvResult.CurrentRow.Index).Value
            Description.Text = dgvResult(dgvResult.Columns("Description").Index, dgvResult.CurrentRow.Index).Value
        Catch ex As Exception

        End Try
    End Sub
    Private Sub btAdd_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAdd.Click
        dTable.Rows.Add(Title.Text, Description.Text)
        dgvResult.DataSource = dTable
    End Sub

    Private Sub btUpdate_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btUpdate.Click
        dgvResult(dgvResult.Columns("Title").Index, dgvResult.CurrentRow.Index).Value = Title.Text
        dgvResult(dgvResult.Columns("Description").Index, dgvResult.CurrentRow.Index).Value = Description.Text

    End Sub
    Private Sub btDelete_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDelete.Click
        If MessageBox.Show("Are you sure you want to delete " & Title.Text & "?", "Delete row", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
            dTable.Rows.RemoveAt(dgvResult.CurrentRow.Index)
            dgvResult.DataSource = dTable
        End If
    End Sub

    Private Sub btSave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click
        Dim ext As String = ""
        sdataset = New DataSet
        sdataset.Tables.Add(dTable)

        If (statusnew = False) Then 'Open
            sdataset.WriteXml(path)
        Else 'New
            _dir = getUserAppFolder("ThungDoNghe", "ToChuc", "")
            Dim SaveFileDialog As New SaveFileDialog
            SaveFileDialog.InitialDirectory = _dir
            SaveFileDialog.Filter = "Text Files (*.org)|*.org|All Files (*.*)|*.*"
            If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
                sdataset.WriteXml(SaveFileDialog.FileName)
                MsgBox("Save success", MsgBoxStyle.Information)
            End If
        End If
    End Sub

    Private Sub frmOrganizatio_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'sdataset = New DataSet
        dTable = New DataTable
        dTable.Columns.Add("Title", GetType(String))
        dTable.Columns.Add("Description", GetType(String))
        If (statusnew = True) Then

        Else
            Dim xmlDatadoc As New XmlDataDocument
            Try
                xmlDatadoc.DataSet.ReadXml(path)
            Catch ex As Exception
                MsgBox("The file isn't format", MsgBoxStyle.Critical)
            End Try

            For Each table As DataTable In xmlDatadoc.DataSet.Tables
                For Each row1 As DataRow In table.Rows
                    dTable.Rows.Add(row1(0), row1(1))
                Next
            Next
            dgvResult.DataSource = dTable
        End If

    End Sub

    Private Sub dgvResult_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvResult.CellContentClick

    End Sub

    Private Sub dgvResult_SelectionChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvResult.SelectionChanged
        Try
            Title.Text = dgvResult(dgvResult.Columns("Title").Index, dgvResult.CurrentRow.Index).Value
            Description.Text = dgvResult(dgvResult.Columns("Description").Index, dgvResult.CurrentRow.Index).Value
        Catch ex As Exception

        End Try
    End Sub
End Class